﻿using System;
using Aptea.OpenXmlOffice;
using DocumentFormat.OpenXml.Spreadsheet;
using eCRAM.Model;
using eCRAM.Tools;

namespace eCRAM.Bll
{
    /// <summary>
    /// Generateur pour l'onglet contenant le tableau de details des crams
    /// </summary>
    public class GraphWorksheetGenerator
    {
        /// <summary>
        /// Predicat des projets à inclure dans le tableau
        /// </summary>
        public Func<CramLine, bool> ProjectPredicate { get; set; }
        /// <summary>
        /// Predicat des congés à inclure dans le tableau
        /// </summary>
        public Func<CramLine, bool> HolidayPredicate { get; set; }
        /// <summary>
        /// Instance du manger excel
        /// </summary>
        public ExcelManager ExcelManager { get; set; }
        /// <summary>
        /// Onglet contenant le tableau
        /// </summary>
        public Worksheet WorkSheet { get; set; }
        /// <summary>
        /// Cram contenant les données à exporter
        /// </summary>
        public Cram Cram { get; set; }
        /// <summary>
        /// Nombre de projets affichables de la tableau
        /// </summary>
        public int NbProject { get; set; }

        /// <summary>
        /// Genére le contenu de l'onglet
        /// </summary>
        public void Generate()
        {
            var firstName = Cram.User.FirstName.Capitalize();
            var lastName = Cram.User.LastName.ToUpperInvariant();

            WorkSheet = ExcelManager.AddWorksheet(string.Format("{0} {1}", firstName, lastName), OrientationValues.Landscape);
            ExcelManager.SetMargins(WorkSheet, new ExcelMargins(0, 0, 0, 0, 0, 0));

            var excelSheetGenerator = new ExcelSheetGenerator
            {
                ExcelManager = ExcelManager,
                WorkSheet = WorkSheet,
                IdxRow = 1,
                NbProject = NbProject,
                ProjectPredicate = ProjectPredicate,
                HolidayPredicate = HolidayPredicate,
                Cram = Cram
            };

            excelSheetGenerator.SetColumnWidth();

            // On écrit l'entete
            excelSheetGenerator.AddHeader();

            // On écrit les infos
            excelSheetGenerator.AddBody();

            // On écrit le pied de page
            excelSheetGenerator.AddFooter();
        }
    }
}
